package com.opgea.crm.services;


import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.common.vo.ActivityVo;
import com.opgea.crm.common.vo.DatePeriodVo;
import com.opgea.crm.domain.entities.Activity;
import com.opgea.crm.domain.entities.Employee;
import com.opgea.crm.domain.modal.QualifierModel;
import com.opgea.crm.repository.ActivityRepo;

@Service
public class ActivityServiceImpl implements ActivityService{

	@Autowired
	ActivityRepo activityRepo;
	
	public void create(Activity activity) {
		activityRepo.create(activity);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void remove(long id) 
	{
		activityRepo.remove(id);
	}
	
	public List<QualifierModel> getActivitySearchCriteriaTypes(){
		List<QualifierModel> activitySearchTypes = new ArrayList<QualifierModel>();
		for(ApplicationConstant.ActivitySearchCriteriaQualifier qualifier : ApplicationConstant.ActivitySearchCriteriaQualifier.values()){
			activitySearchTypes.add(new QualifierModel(Long.valueOf(qualifier.ordinal()), qualifier.name()));
			System.out.println("Activity Search Type : "+qualifier);
		}
		return activitySearchTypes;
	}		

	@Transactional(propagation = Propagation.REQUIRED)
	public Activity find(long id) {
		return activityRepo.find(id);
	}
	
	@Transactional
	public void update(Activity activity) {
		activityRepo.update(activity);
	}
	
	public List<ActivityVo> findAll() {
		//return activityRepo.findAll();
		
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findAll();
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			vo.setId(new Long(activity.getId()));
			if(activity.getActivityType()!= null){
				vo.setActivityType(activity.getActivityType().getFieldValue());
			}
			if(activity.getOpportunity() != null){
				vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
			}
			if(activity.getAssignTo()!=null){
				Employee emp = activity.getAssignTo();
				vo.setSalesPerson(emp.getFirstName()+" "+emp.getLastName());
			}
			if(activity.getCreatedBy() != null){
				Employee createdBy = activity.getCreatedBy().getEmployee();
				vo.setCreatedBy(createdBy.getFirstName()+" "+createdBy.getLastName());				
			}
			if(activity.getActivityStage()!=null){
				vo.setStage(activity.getActivityStage().getFieldValue());
			}
			if(activity.getActivityStatus()!=null){
				vo.setStatus(activity.getActivityStatus().getFieldValue());
			}
			if(activity.getPriority()!=null){
				vo.setPriority(activity.getPriority().getFieldValue());
			}
			if(activity.getLead() != null){
				vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			}
			if(activity.getScheduledDate() != null){
				vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			}
			activities.add(vo);
			System.out.println(vo);
		}
		return activities;		
	}
	
	public List<ActivityVo> findDynamically(String criteria, String value) {
		
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findDynamically(criteria, value);
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			vo.setId(new Long(activity.getId()));
			
			if(activity.getScheduledDate() != null){
				vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			}
			if(activity.getActivityType()!=null){
				vo.setActivityType(activity.getActivityType().getFieldValue());
			}
			if(activity.getLead() !=null){
				vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			}
			if(activity.getOpportunity()!=null){
				vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
			}
			if(activity.getAssignTo()!=null){
				Employee emp = activity.getAssignTo();
				vo.setSalesPerson(emp.getFirstName()+" "+emp.getLastName());
			}
			if(activity.getCreatedBy() != null){
				Employee createdBy = activity.getCreatedBy().getEmployee();
				vo.setCreatedBy(createdBy.getFirstName()+" "+createdBy.getLastName());				
			}
			if(activity.getActivityStage()!=null){
				vo.setStage(activity.getActivityStage().getFieldValue());
			}
			if(activity.getActivityStatus()!=null){
				vo.setStatus(activity.getActivityStatus().getFieldValue());
			}
			if(activity.getPriority()!=null){
				vo.setPriority(activity.getPriority().getFieldValue());
			}			
			
			
			activities.add(vo);
			System.out.println("Activity : "+vo);
		}
		return activities;		
	}	

	public List<ActivityVo> findAllByLeadId(long leadId) {
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findAllByLeadId(new Long(leadId));
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			vo.setId(new Long(activity.getId()));
			
			if(activity.getScheduledDate() != null){
				vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			}
			if(activity.getActivityType()!=null){
				vo.setActivityType(activity.getActivityType().getFieldValue());
				vo.setActivityTypeId(activity.getActivityType().getId());
			}
			if(activity.getLead() !=null){
				vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			}
			if(activity.getOpportunity()!=null){
				vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
				vo.setOpportunityTitleId(activity.getOpportunity().getId());
			}
			if(activity.getAssignTo()!=null){
				vo.setSalesPerson(activity.getAssignTo().getFirstName());
				vo.setSalesPersonId(activity.getAssignTo().getId());
			}
			if(activity.getActivityStage()!=null){
				vo.setStage(activity.getActivityStage().getFieldValue());
				vo.setStageId(activity.getActivityStage().getId());
			}
			if(activity.getActivityStatus()!=null){
				vo.setStatus(activity.getActivityStatus().getFieldValue());
				vo.setStatusId(activity.getActivityStatus().getId());
			}
			if(activity.getPriority()!=null){
				vo.setPriority(activity.getPriority().getFieldValue());
				vo.setPriorityId(activity.getPriority().getId());
			}
			if(activity.getCreatedBy() != null){
				Employee createdBy = activity.getCreatedBy().getEmployee();
				vo.setCreatedBy(createdBy.getFirstName()+" "+createdBy.getLastName());
			}

			//vo.setReminder()
			if(activity.getReminderDate() != null){
				vo.setReminderDate(DateUtil.dateToString(activity.getReminderDate()));
			}
			if(activity.getSubject() != null){
				vo.setSubject(activity.getSubject());
			}
			if(activity.getDuration() != null){
				vo.setDuration(activity.getDuration());
			}
			if(activity.getNote() != null){
				vo.setActivityNotes(activity.getNote());
			}
			activities.add(vo);
			System.out.println("Activity : "+vo);
		}
		return activities;
	}

	public List<ActivityVo> findAllByActivityType(long leadId, int activityType) {
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findAllByActivityType(new Long(leadId), activityType);
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			vo.setId(activity.getId());
			
			if(activity.getScheduledDate() != null){
				vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			}
			if(activity.getActivityType() != null){
				vo.setActivityType(activity.getActivityType().getFieldValue());
			}
			if(activity.getLead() != null){
				vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			}
			if(activity.getOpportunity() != null){
				vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
			}
			if(activity.getAssignTo() != null){
				Employee assignedTo = activity.getAssignTo();
				vo.setSalesPerson(assignedTo.getFirstName()+" "+assignedTo.getLastName());
			}
			if(activity.getCreatedBy() != null){
				Employee createdBy = activity.getCreatedBy().getEmployee();
				vo.setCreatedBy(createdBy.getFirstName()+" "+createdBy.getLastName());
			}
			if(activity.getActivityStage() != null){
				vo.setStage(activity.getActivityStage().getFieldValue());
			}
			if(activity.getActivityStatus() != null){
				vo.setStatus(activity.getActivityStatus().getFieldValue());
			}
			if(activity.getPriority() != null){
				vo.setPriority(activity.getPriority().getFieldValue());
			}
			activities.add(vo);
			System.out.println("Activity : "+vo);
		}
		return activities;
	}
	
	@Override
	public List<ActivityVo> findAllByDuration(Long assignedToId, Date from, Date to) {
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findAllByDuration(assignedToId, from, to);
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			vo.setId(new Long(activity.getId()));
			
			if(activity.getScheduledDate() != null){
				vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			}
			if(activity.getActivityType() != null){
				vo.setActivityType(activity.getActivityType().getFieldValue());
			}
			if(activity.getLead() != null){
				vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			}
			if(activity.getOpportunity() != null){
				vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
			}
			if(activity.getAssignTo() != null){
				Employee assignedTo = activity.getAssignTo();
				vo.setSalesPerson(assignedTo.getFirstName()+" "+assignedTo.getLastName());
			}
			if(activity.getCreatedBy() != null){
				Employee createdBy = activity.getCreatedBy().getEmployee();
				vo.setCreatedBy(createdBy.getFirstName()+" "+createdBy.getLastName());
			}
			if(activity.getActivityStage() != null){
				vo.setStage(activity.getActivityStage().getFieldValue());
			}
			if(activity.getActivityStatus() != null){
				vo.setStatus(activity.getActivityStatus().getFieldValue());
			}
			if(activity.getPriority() != null){
				vo.setPriority(activity.getPriority().getFieldValue());
			}
			activities.add(vo);
			System.out.println("Activity : "+vo);
		}
		return activities;
	}	
	
	public DatePeriodVo findPeriod(String periodQualifier){
		
		DatePeriodVo datePeriodVo = null;
		Calendar cal = GregorianCalendar.getInstance();
		int year = cal.getTime().getYear();
		int month = cal.getTime().getMonth();
		int date = cal.getTime().getDate();
		
		System.out.println(year+" "+month+" "+date);
		NumberFormat formatter = new DecimalFormat("00"); 
		

		String currentYear = String.valueOf(year+1900);
		String currentMonth = String.valueOf(formatter.format(month+1));
		String currentDate = String.valueOf(formatter.format(date));
		
		String lastYear = String.valueOf(year+1900-1);
		String lastMonth = String.valueOf(formatter.format(month));
		int quarter = getQuarter(month);

		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.Custom.ordinal()){
			datePeriodVo = new DatePeriodVo("", "");
		}
		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.CurrentYear.ordinal()){
			datePeriodVo = new DatePeriodVo(currentYear+"-01-01", currentYear+"-12-31");
		}
		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.LastYear.ordinal()){
			datePeriodVo = new DatePeriodVo(lastYear+"-01-01", lastYear+"-12-31");
		}
		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.LastMonth.ordinal()){
			if(month == 0){
				datePeriodVo = new DatePeriodVo(lastYear+"-12-01", lastYear+"-12-31");
			}
			else{
				datePeriodVo = new DatePeriodVo(currentYear+"-"+lastMonth+"-01", currentYear+"-"+lastMonth+"-30");
			}
		}
		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.CurrentQuarter.ordinal()){
			if(quarter == 1){
				datePeriodVo = new DatePeriodVo(currentYear+"-01-01", currentYear+"-03-31");
			}
			if(quarter == 2){
				datePeriodVo = new DatePeriodVo(currentYear+"-04-01", currentYear+"-06-30");
			}
			if(quarter == 3){
				datePeriodVo = new DatePeriodVo(currentYear+"-07-01", currentYear+"-09-30");
			}
			if(quarter == 4){
				datePeriodVo = new DatePeriodVo(currentYear+"-10-01", currentYear+"-12-31");
			}			
		}
		if(Integer.valueOf(periodQualifier) == ApplicationConstant.PeriodQualifier.LastQuarter.ordinal()){
			quarter = quarter -1;
			if(quarter == 1){
				datePeriodVo = new DatePeriodVo(currentYear+"-01-01", currentYear+"-03-31");
			}
			if(quarter == 2){
				datePeriodVo = new DatePeriodVo(currentYear+"-04-01", currentYear+"-06-30");
			}
			if(quarter == 3){
				datePeriodVo = new DatePeriodVo(currentYear+"-07-01", currentYear+"-09-30");
			}
			if(quarter == 4){
				datePeriodVo = new DatePeriodVo(currentYear+"-10-01", currentYear+"-12-31");
			}
		}		
		return datePeriodVo;
	}
	
	public int getQuarter(int month){
			int quarter = 0;
			if(month <= 2){
				quarter = 1;
			}
			if(month > 2 && month <= 5){
				quarter = 2;
			}
			if(month > 5 && month <= 8){
				quarter = 3;
			}
			if(month > 8 && month <= 11){
				quarter = 4;
			}
		return quarter;
	}

	public List<ActivityVo> findReport(String periodFrom, String periodTo,
			String statusType, String priority, String activityType) {
		List<ActivityVo> activities = new ArrayList<ActivityVo>();
		List<Activity> activityList = activityRepo.findReport(periodFrom, periodTo, statusType, priority, activityType);
		for(Activity activity : activityList){
			ActivityVo vo = new ActivityVo();
			System.out.println("Activity : "+activity);
			if(activity.getActivityType()!= null)
			vo.setActivityType(activity.getActivityType().getFieldValue());
			
			
			vo.setContactTo(activity.getLead().getFirstName()+" "+activity.getLead().getLastName());
			vo.setDueDate(DateUtil.dateToString(activity.getScheduledDate()));
			if(activity.getId() !=null)
			vo.setId(new Long(activity.getId()));
			if(activity.getOpportunity() != null)
			vo.setOpportunityTitle(activity.getOpportunity().getOpportunityTitle());
			if(activity.getAssignTo()!=null)
			vo.setSalesPerson(activity.getAssignTo().getFirstName());
			if(activity.getActivityStage()!=null)
			vo.setStage(activity.getActivityStage().getFieldValue());
			if(activity.getActivityStatus()!=null)
			vo.setStatus(activity.getActivityStatus().getFieldValue());
			if(activity.getPriority()!=null)
			vo.setPriority(activity.getPriority().getFieldValue());
			activities.add(vo);
		}
		return activities;	
	}

	
}
